<template>
    <el-cascader :id="id" :props="options" :style="styles" v-model="currentValue" :disabled="disabled"></el-cascader>
</template>
<script>
import Vue  from 'vue'
let sef=this;
export default {
    props:['id','styles','value','data','extype','isreadonly'],
    name: "user-select",
    data(){
        return {
           currentValue:this.value,
            disabled:false,
            options: {
                lazy: true,
                disabled:false,
                lazyLoad (node, resolve) { 
                    let level=node.level;
                    
                    if(level==0){
                         sef.$post('/userApi/getDepart',{}).then(data=>{
                           
                            
                            resolve(data);
                        })
                    }else{
                        let label=node.label;
                        
                        let data={};
                        data.depart=label;
                        sef.$post("/userApi/getDepartUser",data).then(data=>{
                             resolve(data);
                        })
                    }
                    
                }
            }
        }
    }, 
    beforeCreate(){
        sef=this;
    },
    created(){
        this.setData();
        if(this.isreadonly=='Y')
            this.disabled=true;
        
    },
    methods:{
        setData(){
            if(!this.value){
                if(this.data){
                    let ids=this.id.split(".");
                    if(ids.length>1){
                        let p=ids[0];
                        let id_=ids[1];
                        if(this.index||this.index=='0'){
                            try{
                                this.currentValue=unescape(this.data[p][this.index][id_]).split(",");
                            }catch(e){}
                        }else{
                            try{
                                this.currentValue=unescape(this.data[p][id_]).split(",");
                            }catch(e){}
                        }
                        
                    }else{
                        if(this.data[this.id])
                            this.currentValue= unescape(this.data[this.id]).split(",");
                    }
                
                }
            }else{
                
                let data={};
                data.id=this.id;
                data.val=this.value;
                data.index=this.index;
                this.$emit('input',JSON.stringify(data));
            }
        }
        
    },
    watch:{
        currentValue: function (val) { //currentValue值变动就向父组件传值
            let vals="";
            for(let i=0;i<val.length;i++){
                vals=vals=="" ? val[i]:vals+","+val[i];
            }

            let data={};
            data.id=this.id;
            data.val=vals;
            
            this.$emit('input',JSON.stringify(data));
            this.$emit('on-change',JSON.stringify(data));


            let val1=val[0];
            let val2=val[1];
            if(val2!=undefined&&val2!="undefined"){
                let extypes=this.extype.split("#");
                let data3={};
                data3.id=extypes[0];
                data3.val=val1;
                data3.index=this.index;
                this.$emit('input',JSON.stringify(data3));
                this.$emit('on-change',JSON.stringify(data3));
                console.log(data3);
                
                let data2={};
                data2.id=extypes[1];
                data2.val=val2;
                data2.index=this.index;
                this.$emit('input',JSON.stringify(data2));
                this.$emit('on-change',JSON.stringify(data2));
                console.log(data2);
            }
            
        },
        value:function (val) { //对值进行验证
            
        }
    }
}
</script>